<script setup lang="ts">
// 定义组件名
defineOptions({
  name: 'RegisterPage'
})
import { registerByAPI } from '@/apis/user';
import router from '@/router';
import { useUserStore } from '@/stores';
import { codeRules, mobileRules, passwordRules } from '@/utils/rules';
import { showFailToast, showSuccessToast } from 'vant';
import { ref } from 'vue';

const mobile = ref('')
const password = ref('')
const code = ref('')
const isAgree = ref(false)


// 发送短信验证码
import { userMobileCode } from '@/composables/useMobileCode' 
// 调用组合式函数
const {onSend, time, formRef} = userMobileCode(mobile, 'register')

// 注册按钮
const login = async () => {
  if(!isAgree.value) return showFailToast('请勾选我已同意')
    // 验证完毕进行登录
    // console.log('111')
    // 发送请求
    // 判断是密码登录还是验证码登录
    const res = await registerByAPI(mobile.value, code.value, password.value)
    console.log(res)
    const store = useUserStore()
    store.setUser(res.data)
    showSuccessToast('注册成功')
    router.push('/login')
}
 // 小眼睛点击显示密码
const isShow = ref(false)

</script>

<template>
  <div class="login-page">
    <cp-nav-bar
      right-text="登录"
      @click-right="router.push('/login')"
    ></cp-nav-bar>
    <!-- 头部 -->
    <div class="login-head">
      <h3>注册</h3>
    </div>
    <!-- 表单 -->
    <van-form autocomplete="off" @submit="login" ref="formRef">
      <van-field placeholder="请输入手机号" type="tel" :rules="mobileRules" v-model="mobile" name="mobile" ></van-field>
      <van-field placeholder="请输入短信验证码" :rules="codeRules" v-model="code">
        <template #button>
            <span class="btn-send" @click="onSend" :class="{ active: time > 0  }">{{time > 0 ? `${time}秒后重新发送验证码` : '发送验证码' }}</span>
        </template>
      </van-field>
      <van-field placeholder="请输入密码" :type="`${isShow ? 'text' : 'password'}`" :rules="passwordRules" v-model="password">
        <template #button>
            <cp-icons class="icon-eye" @click="isShow = !isShow" :name="`${isShow ? 'login-eye-on' : 'login-eye-off' }`"></cp-icons>
        </template>
      </van-field>
      <div class="cp-cell">
        <van-checkbox v-model="isAgree">
          <span>我已同意</span>
          <a href="javascript:;">用户协议</a>
          <span>及</span>
          <a href="javascript:;">隐私条款</a>
        </van-checkbox>
      </div>
      <div class="cp-cell">
        <van-button block round type="primary" native-type="submit" >注 册</van-button>
      </div>
    </van-form>
  </div>
</template>

<style lang="scss" scoped>
.icon-eye {
    margin-right: 10px;
  }
.login {
  &-page {
    padding-top: 46px;
  }
  &-head {
    display: flex;
    padding: 30px 30px 50px;
    justify-content: space-between;
    align-items: flex-end;
    line-height: 1;
    h3 {
      font-weight: normal;
      font-size: 24px;
    }
    a {
      font-size: 15px;
    }
  }
  &-other {
    margin-top: 60px;
    padding: 0 30px;
    .icon {
      display: flex;
      justify-content: center;
      img {
        width: 36px;
        height: 36px;
        padding: 4px;
      }
    }
  }
}
.van-form {
  padding: 0 14px;
  .cp-cell {
    height: 52px;
    line-height: 24px;
    padding: 14px 16px;
    box-sizing: border-box;
    display: flex;
    align-items: center;
    .van-checkbox {
      a {
        color: var(--cp-primary);
        padding: 0 5px;
      }
    }
  }
  .btn-send {
    color: var(--cp-primary);
    &.active {
      color: rgba(22,194,163,0.5);
    }
  }
}
</style>